home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / builtin / structure.c < prev    next >
C/C++ Source or Header  |  1991-05-21  |  6KB  |  205 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* structure.c */
  25.  
  26. #include "builtin.h"
  27.  
  28. extern LONG_PTR insert();
  29. static BYTE perm = PERM;
  30.  
  31. b_ARG0()  /* (I, T, X) */
  32. {
  33.    /* reg1 is bound to a number; reg2 is bound to a structure;
  34.     * reg3 may be free or bound; no checking for above is done;
  35.     * reg1 out of range will cause failure
  36.     */
  37.  
  38.    register LONG     op1, op2;
  39.    register LONG_PTR top;
  40.  
  41.    op1 = reg[1];  DEREF(op1);
  42.    if (!ISINTEGER(op1)) {
  43.       printf("arg0: Index must be an integer.\n");
  44.       FAIL0;
  45.       return;
  46.    }
  47.    op1 = INTVAL(op1);
  48.  
  49.    op2 = reg[2];  DEREF(op2);
  50.    if (ISCONSTR(op2) && op1 <= GET_STR_ARITY(op2) && op1 > 0)
  51.       if (unify(*(((LONG_PTR)(UNTAG(op2))) + op1), reg[3]))
  52.          return;
  53.    if (ISLIST(op2) && op1 <= 2)
  54.       if (unify(*(((LONG_PTR)(UNTAG(op2))) + op1 - 1), reg[3]))
  55.          return;
  56.    FAIL0;
  57. }
  58.  
  59. b_ARITY()  /* (T, N) */
  60. {
  61.    /* reg1 is bound to a structure or constant; reg2 will be unified with
  62.     * the arity of reg1
  63.     */
  64.  
  65.    register LONG     op1, op2;
  66.    register LONG_PTR top;
  67.  
  68.    op1 = reg[1];  DEREF(op1);
  69.    if (ISCONSTR(op1))
  70.       op2 = MAKEINT(GET_ARITY(GET_STR_PSC(op1)));
  71.    else if (ISLIST(op1))
  72.       op2 = MAKEINT(2);
  73.    else if (ISNUM(op1))
  74.       op2 = MAKEINT(0);
  75.    else {
  76.       printf("arity: Term must be bound.\n");
  77.       FAIL0;
  78.       return;
  79.    }
  80.    if (!unify(reg[2], op2))
  81.       {FAIL0;}
  82. }
  83.  
  84. b_FUNCTOR0()  /* (T, F) */
  85. {
  86.    /* reg1 is bound to a structure (no checking), reg2 will be unified
  87.     * with the functor of reg1
  88.     */
  89.  
  90.    register LONG     op1, op2;
  91.    register LONG_PTR top;
  92.    PSC_REC_PTR       psc_ptr;
  93.  
  94.    op1 = reg[1];  DEREF(op1);
  95.    if (ISLIST(op1))
  96.       op2 = (LONG)insert(".", 1, 0, &perm);
  97.    else {
  98.       psc_ptr = GET_STR_PSC(op1);
  99.       if (GET_ARITY(psc_ptr) != 0)
  100.          op2 = (LONG)insert(GET_NAME(psc_ptr), GET_LENGTH(psc_ptr), 0, &perm);
  101.       else op2 = op1;
  102.    }
  103.    op2 |= CS_TAG;
  104.    if (!unify(op2, reg[2]))
  105.       {FAIL0;}
  106. }
  107.  
  108. b_BLDSTR()  /* (F, N, T) */
  109. {
  110.    /* reg1 is bound to an atom, reg2 is bound to an integer (> 0)
  111.     * reg3 is free, and will be set to the most general structure
  112.     * with functor reg1; incomplete error checking
  113.     */
  114.  
  115.    register LONG     op1, op2;
  116.    register LONG_PTR top;
  117.    PSC_REC_PTR       psc_ptr;
  118.    LONG              i, a;
  119.    LONG_PTR          stack_top;
  120.  
  121.    op1 = reg[1];  DEREF(op1);
  122.    if (!ISCONSTR(op1)) {
  123.       printf("bldstr: first arg must be constant\n");
  124.       FAIL0;
  125.       return;
  126.    }
  127.    op2 = reg[2];  DEREF(op2);
  128.    if (!ISINTEGER(op2)) {
  129.       printf("bldstr: second arg must be integer\n");
  130.       FAIL0;
  131.       return;
  132.    }
  133.    a = INTVAL(op2);
  134.    if (a < 0 || a > 127) {
  135.       printf("bldstr: arity must be between 0 and 127\n");
  136.       FAIL0;
  137.       return;
  138.    }
  139.    op2 = reg[3];  DEREF(op2);
  140.    psc_ptr = GET_STR_PSC(op1);
  141.    if (GET_ARITY(psc_ptr) > 0) {
  142.       printf("bldstr: first arg must be constant\n");
  143.       FAIL0;
  144.       return;
  145.    }
  146.    /* check for heap overflow */
  147.    stack_top = (breg < ereg) ? breg : ereg - ENV_SIZE(cpreg);
  148.    if (stack_top < hreg + a + 1) {
  149.       /* garbage_collection("b_BLDSTR"); */
  150.       if (stack_top < hreg + a + 1)    /* still too full */
  151.      quit("Heap overflow\n");
  152.    }
  153.    if (a == 2 && GET_NAME(psc_ptr)[0] == '.' && GET_LENGTH(psc_ptr) == 1)
  154.       FOLLOW(op2) = (LONG)hreg | LIST_TAG;
  155.    else if (a == 0)
  156.       FOLLOW(op2) = op1;
  157.    else {
  158.       op1 = (LONG)insert(GET_NAME(psc_ptr), GET_LENGTH(psc_ptr), a, &perm);
  159.       op1 = FOLLOW(op1);        /* returns addr of addr of psc */
  160.       FOLLOW(op2) = (LONG)hreg | CS_TAG;
  161.       *hreg++ = op1;
  162.    }
  163.    PUSHTRAIL(op2);
  164.    for (i = 0; i < a; hreg++, i++)
  165.       MAKE_FREE(LONG, *hreg);
  166. }
  167.  
  168. b_MKSTR()
  169. {        /*  (F, T, A) : F is a pointer to the PSC table entry of a function
  170.         symbol f/n.  b_MKSTR creates T, a most general instance of
  171.         a term with principal functor f/n on the heap, and sets the
  172.         variable register 2 points to,  to it; the variable that
  173.         register 3 points to is set to the arity n.
  174.  
  175.             At this point, this is used only for decompilation.   */
  176.  
  177.     int i, arity;
  178.     PSC_REC_PTR psc_p;
  179.     register LONG op;
  180.     register LONG_PTR top;
  181.  
  182.     op = reg[1]; DEREF(op);
  183.     if (!ISCONSTR(op)) {
  184.     printf("mkstr: first arg must be a function symbol!\n");
  185.     FAIL0; return; }
  186.     psc_p = (PSC_REC_PTR)(UNTAGGED(op));
  187.     arity = GET_ARITY(psc_p);
  188.     op = reg[2]; DEREF(op);
  189.     if (!ISFREE(op)) {
  190.     printf("mkstr: second argument must be a variable!\n");
  191.     FAIL0; return; }
  192.     if ((arity==2) && (GET_NAME(psc_p)[0]=='.') && (GET_LENGTH(psc_p)==1)) 
  193.     FOLLOW(op) = (LONG)hreg | LIST_TAG;
  194.     else {
  195.     FOLLOW(op) = (LONG)hreg | CS_TAG;
  196.     *hreg++ = (LONG)psc_p;
  197.     if (arity > 0)
  198.         for (i = 0; i < arity; hreg++, i++) MAKE_FREE(LONG, *hreg);
  199.     };
  200.     PUSHTRAIL(op);
  201.     op = reg[3]; DEREF(op);
  202.     FOLLOW(op) = MAKEINT(arity);
  203.     PUSHTRAIL(op);
  204. }
  205.